// TOWN SCRIPT
//    Town 8: Imperial Palace

// This is the special encounter script for this town.
// The states INIT_STATE, EXIT_STATE, and START_STATE have
// meanings that are described in the documenation. States you write
// yourself should be numbered from 10-100.

begintownscript;

variables;

short i,j,k,r1,choice;
string dlgstr;

body;

beginstate INIT_STATE;
	change_blocked(10,15,1);
	change_blocked(11,15,1);
	change_blocked(12,15,1);
	change_blocked(13,15,1);
	
	change_blocked(10,16,1);
	change_blocked(11,16,1);
	change_blocked(12,16,1);
	
	set_name(7,"Imperial Guard");
	set_name(6,"Xiang Infantry");
break;

beginstate EXIT_STATE;
// Always called when the town is left.
break;

beginstate START_STATE;
// This state is called every turn the party is in this town.
break;

beginstate 11;
	if (get_flag(8,23) == 250)
		end();
		
	reset_dialog();
	add_dialog_str(0,"You've reached the core of the Imperial Palace, and not a moment too soon.",0);
	add_dialog_str(1,"From below the ladderway that lead you to this area, you can hear the shouts of guards preparing for the worst. In a matter of hours, a state of war will exist between the Empire of Huan and the Empire of Sujiro.",0);
	add_dialog_str(2,"That is, if you can't get to the Emperor with the information you've gotten from Xiang first. The ambassador in Tian Po kept thorough records; far too thorough for his own good.",0);
	add_dialog_str(3,"Although the journal itself was in code and diplomatically worthless, it suggested strongly that records of the attack on the Fifth Regiment existed in fine detail in the now-abandoned embassy in the Imperial Palace.",0);
	add_dialog_str(4,"It also detailed the presence of a secret room in which the records were hidden. Which explains why the Imperial forces have not yet exonerated Sujiro from their apparent wrongdoings.",0);
	add_dialog_str(5,"Your passkey, much like a small version of the Huan coat-of-arms, is cold in your pocket. With it and the important documents in the Embassy, you'll be able to see the Emperor and set things right once and for all.",0);
	add_dialog_choice(0,"OK.");
	choice = run_dialog(1);
	set_flag(8,23,250);
break;

beginstate 15;
	reset_dialog_preset_options(8);
	if (run_dialog(1) == 2) {
		print_str("The chains rattle. The alarms are off.");
		play_sound(99);
		set_flag(1,2,1);
		}
break;

beginstate 18;
	if (get_flag(1,2) == 0) {
		message_dialog("You stop quickly. You remember that, the last time you were here, you were warned that entering the inner antechamber without having activated the Imperial Passkey would trip an alarm.","As much as you enjoy getting riddled with bolts, you think you'll pass on it today. The slot in which the key can be inserted is back down the hall.");
		block_entry(1);
		end();
		}
	
	set_flag(1,2,0);
	change_spec_item(27,-1);
	message_dialog("You approach the Emperor with your evidence. You can only hope that you've reached him in time.","");
	end_scenario(1);
break;

beginstate 19;
	if (get_flag(8,25) == 250)
		end();
	set_flag(8,25,250);
	message_dialog("When you enter this room, a man clad in the black of the Xiang Special Infantry swings down from the ceiling. He is carrying several firebombs developed in the Xiang research facilities, the ones you read about in the citadel in Bai-Jong!","");
	activate_hidden_group(5);
	
	put_boom_on_char(6,3,0);
	run_animation_sound(25);
	
	text_bubble_on_char(6,"It ends here!");
	force_instant_terrain_redraw();
	pause(15);
	
	text_bubble_on_char(6,"");
	text_bubble_on_char(6,"I cannot allow you");
	force_instant_terrain_redraw();
	pause(15);
	
	text_bubble_on_char(6,"");
	text_bubble_on_char(6,"to reach the Emperor!");
	force_instant_terrain_redraw();
	pause(15);
	
	text_bubble_on_char(6,"");
	text_bubble_on_char(6,"For Xiang and the Black Iron Fist!");
	force_instant_terrain_redraw();
	pause(15);
break;

beginstate 20;
	if (get_flag(8,27) == 250)
		end();
		
	set_flag(8,27,250);
	change_spec_item(4,1);
	reset_dialog();
	add_dialog_str(0,"Sure enough, below a loose floorboard are a stack of papers detailing plans for and execution of an attack by disguised agents of Xiang on the Fifth Regiment along the Madara River.",0);
	add_dialog_str(1,"With these, the Emperor will have no choice but to switch hostilities from Sujiro to Xiang, and the northern dogs will learn a harsh lesson in stabbing Huan in the back.",0);
	add_dialog_str(2,"The black steel key is here, too: this will open the gates to the northwest.",0);
	set_flag(8,11,run_dialog(1));
break;

beginstate 25;
if (char_ok(7)) {
	block_entry(1);
	while (char_ok(i) == 0)
		{i = i + 1; }
	clear_buffer();
	append_string("The guard lowers his sword, blocking your way. _I already told you,");
	append_char_name(i);
	append_string(",_ he says, _you cannot see the Emperor today. Come back tomorrow._");
	get_buffer_text(dlgstr);
	message_dialog(dlgstr,"");
	}
else {
	change_blocked(10,16,0);
	change_blocked(11,16,0);
	change_blocked(12,16,0);
	}
break;

beginstate 26;
if (get_flag(8,10) == 0) {
	set_flag(8,10,1);
	message_dialog("Here he lies: the ambassador in Tian Po, who committed suicide to preserve his honor after having been forced to betray his own country. If the journal that you read was correct, the records are in the box.","");
	}
break;

beginstate 27;
if (get_terrain(19,9) != 29)
	end();

	if (get_flag(8,11) == 0) {
		message_dialog("Of all the trivial things! The gates to the Inner Imperial Chambers are locked, and you don't have the key.","You can't turn back now. It must be around here somewhere.");
		}
	else {
		message_dialog("You use the black steel key you found with the papers to unlock these gates and open them. You are drawing nearer to the Emperor: his inner chambers lie just beyond this hall.","");
		set_terrain(19,8,33);
		set_terrain(19,9,33);
		set_terrain(19,10,33);
		}
break;

beginstate 28;
	message_dialog("You can't turn back now; the safety and security of the Empire of Huan depends on you reaching the Emperor in time.","");
	block_entry(1);
break;

beginstate 29;
	while (char_ok(i) == 0)
		{i = i + 1; }
	clear_buffer();
	append_string("_Curse you, ");
	append_char_name(i);
	append_string("!_ the Xian assassin roars at you as he falls, mortally wounded.");
	get_buffer_text(dlgstr);
	message_dialog(dlgstr,"_It does not matter! You are too late! Huan is betrayed, and it will fall!_ With that, he dies, and you must hurry to make sure that he is wrong.");
break;

beginstate 30;
	message_dialog("The Xiang Special Infantryman throws a firebomb on the ground. His specially-designed heat shield protects him from the flames, but you are not so lucky.","");
break;